package com.example.bookapp.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class EditPwdActivity extends AppCompatActivity {

    EditText etOld, etNew;
    String oldPwd, newPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_pwd);
        setTitle("修改密码");

        etOld = findViewById(R.id.etP1);
        etNew = findViewById(R.id.etP2);

        findViewById(R.id.btEdit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkNull()) {
                    if (oldPwd.equals(SPUtil.getData("password", ""))) {
                        SPUtil.putData("password", newPwd);
                        toast("修改成功");
                        finish();
                    } else {
                        toast("原密码错误，修改失败");
                    }
                }
            }
        });

    }


    boolean checkNull() {
        oldPwd = etOld.getText().toString();
        newPwd = etNew.getText().toString();
        if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd)) {
            toast("请输入密码");
            return false;
        }
        return true;
    }

    void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}